﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace Controllers
{
    public class VacationController : BaseController
    {
        //分页查询
        public ActionResult Find(int start, int limit, string idStr,string name)
        {
            int empId;
            IList<int> deptIdList = getAuthedDeptIds("Vacation", idStr, out empId);
            EmpBasicInfo ebi = Session["ebi"] as EmpBasicInfo;

            IQueryable<VacationDTO> q = from r in ctx.ReqVacationMains
                                        where r.StatusId != 0 || r.EmpId == ebi.Id
                                           orderby r.FormNo descending
                                           select new VacationDTO
                                          {
                                              FormNo = r.FormNo,
                                              EmpId = r.EmpId,
                                              EmpName = r.Employee.Name,
                                              VacationType = r.VacationType,
                                              DaysCnt=r.DaysCnt,
                                              Cause = r.Cause,
                                              StatusId = r.StatusId
                                              //CheckEmpId = x.CheckEmpId
                                          };
            if (empId != 0)
            {
                q = q.Where(r => r.EmpId == empId);
            }
            else
            {
                q = q.Where(r => (from r1 in ctx.EmpDepPositions where deptIdList.ToArray().Contains(r1.DeptId) select r1.EmpId).Contains(r.EmpId));
            }
            if (!string.IsNullOrWhiteSpace(name))
            {
                q = q.Where(r => r.EmpName.Contains(name));
            }
            int totalCnt = q.Count();
            IList<VacationDTO> list = q.Skip(start).Take(limit).ToList();
            foreach (var dto in list)
            {
                var subList = from r in ctx.ReqVacationSubs where r.FormNo == dto.FormNo select r;
                DateTime dtStart = DateTime.MaxValue;
                DateTime dtEnd = DateTime.MinValue;
                foreach (var v in subList)
                {
                    if (dtStart > v.PreStartTime)
                    {
                        dtStart = v.PreStartTime;
                    }
                    if (dtEnd < v.PreEndTime)
                    {
                        dtEnd = v.PreEndTime;
                    }
                }
                dto.PreStartTime = dtStart;
                dto.PreEndTime = dtEnd;

                var signList = from r in ctx.SignFlows where r.FormNo == dto.FormNo orderby r.OrderNo select r;
                foreach(SignFlow sf in signList)
                {
                    if(sf.IsCurrent==1)
                    {
                        dto.CheckEmpId = sf.CheckEmpId;
                        break;//找到第一个签核人后跳出循环
                    }
                }
            }
            //IQueryable<ReqVehicleMains> list = from r in ctx.ReqVehicleMains select r;
            CJson CJson = new CJson();
            string json = CJson.ToJsonString("totalCount:" + totalCnt + ",data", list);
            return Content(json);
        }

        //删除明细
        public string DelDetail(int id)
        {
            string retStr = "Succeed";
            ReqVacationSub oriObj = ctx.ReqVacationSubs.Single(r => r.Id == id);
            ctx.ReqVacationSubs.DeleteOnSubmit(oriObj);
            try
            {
                ctx.SubmitChanges();
            }
            catch (Exception ex)
            {
                retStr = ex.Message;
            }
            return retStr;
        }

        //删除申请,含明细
        public ActionResult Delete(string formNo)
        {
            var rdto = new ResultMessages();
            rdto.Message = "";
            ReqVacationMain oriObj = ctx.ReqVacationMains.Single(r => r.FormNo == formNo);
            ctx.ReqVacationSubs.DeleteAllOnSubmit(oriObj.ReqVacationSubs);
            ctx.ReqVacationMains.DeleteOnSubmit(oriObj);
            try
            {
                ctx.SubmitChanges();
                rdto.Message = "删除成功";
                rdto.Result = true;
            }
            catch
            {
                rdto.Message = "删除失败";
                rdto.Result = false;
            }
            return Json(rdto);
        }

        //申请销单()
        public ActionResult Logout(string json)
        {
            var rdto = new ResultMessages();
            rdto.Message = "";
            JavaScriptSerializer jss = new JavaScriptSerializer();
            ReqVacationMain obj = jss.Deserialize<ReqVacationMain>(json);
            try
            {
                ReqVacationMain oriObj = ctx.ReqVacationMains.Single(r => r.FormNo == obj.FormNo);
                oriObj.RealStartTime = obj.RealStartTime;
                oriObj.RealEndTime = obj.RealEndTime;
                oriObj.Remarks = obj.Remarks;
                oriObj.StatusId = obj.StatusId;

                ctx.SubmitChanges();
                rdto.Message = "更新成功";
                rdto.Result = true;
            }
            catch (Exception ex)
            {
                rdto.Message = "更新失败";
                rdto.Result = false;
            }
            return Json(rdto);
        }
    }
}